package com.juyu.miliao.api.dao.mysql;

import com.juyu.miliao.api.domain.UserVip;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class UserVipDao {
    @Autowired
    private SqlSession sqlSession;


    public UserVip getUserVipById(long userId) {
        return sqlSession.selectOne("getUserVipById", userId);
    }

    public List<UserVip> getUserVipListByVipLevel() {
        return sqlSession.selectList("getUserVipListByVipLevel");
    }

    public int insertUserVip(UserVip entity) {
        return sqlSession.insert("insertUserVip", entity);
    }

    /**
     * 更新userVip 有效时间
     *
     * @param vipDate vipDate
     * @return int 更新条数
     */
    public int updateUserVipDay(int vipDate, int userVipId) {
        Map<String, Object> map = new HashMap<>(4);
        map.put("vipDate", vipDate);
        map.put("userVipId", userVipId);
        return sqlSession.update("updateUserVipDay", map);
    }

    /**
     * 获取未过期的userVip
     *
     * @param userId userId
     * @return UserVip
     */
//    public UserVip getUnpastUserVip(long userId) {
//        return sqlSession.selectOne("getUnpastUserVip", userId);
//    }

    /**
     * 取用户信息的时候判断
     */
    public int updateUserVipStatus(long userId) {
        return sqlSession.update("updateUserVipStatus", userId);
    }

    public List<UserVip> getUserVipListByStatus() {
        return sqlSession.selectList("getUserVipListByStatus");
    }

    public int updateUserVipDate(int userVipId) {
        return sqlSession.update("updateUserVipDate", userVipId);
    }

    public int updateUserVipStatusByUserVipId(int userVipId) {
        return sqlSession.update("updateUserVipStatusByUserVipId", userVipId);
    }

    public UserVip getUserVipByUserIdAndVipType(long userId, int vipType) {
        Map<String,Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("vipType",vipType);
        return sqlSession.selectOne("getUserVipByUserIdAndVipType",map);
    }
}
